package com.obas.commands {
  import com.ease.util.events.MethodReturnEvent;
  import com.ease.framework.ViewRegistry;
  import com.obas.events.RequestChangePasswordEvent;
  import com.obas.events.RequestPluginsEvent;
  import com.obas.events.RequestStageMenuDataEvent;
  import com.obas.events.RequestUserSessionEvent;
  import com.ease.flex.remoting.ServiceManagerComObasNetworkFlexRemoting;
  import com.ease.flex.remoting.ServiceManagerComEaseBusinessFlexRemoting;
  
  import flash.events.EventDispatcher;

  [Event(name="{com.obas.events.RequestStageMenuDataEvent.RETURN_ID}",
      type="com.obas.events.RequestStageMenuDataEvent")]

  [Event(name="{com.obas.events.RequestPluginsEvent.RETURN_ID}",
      type="com.obas.events.RequestPluginsEvent")]

  [Event(name="{com.obas.events.RequestChangePasswordEvent.RETURN_ID}",
      type="com.obas.events.RequestChangePasswordEvent")]

  [Event(name="{com.obas.events.RequestUserSessionEvent.RETURN_ID}",
      type="com.obas.events.RequestUserSessionEvent")]

  [Event(name="{com.obas.flex.common.event.SessionTimeoutEvent.SESSION_TIMEOUT}",
      type="com.obas.flex.common.event.SessionTimeoutEvent")]

  public class DataOperationCommand extends EventDispatcher {

     private static function prepareParamArray(args:Array):Array {
        var result:Array = args;
        if (result == null) {
           return [null];
        }
        else {
           result.push(null);
           return result;
        }
     }

     [RequestHandler("{com.obas.events.RequestUserSessionEvent.REQUEST_ID}")]
     public static function onRequestUserSessionEvent(request:RequestUserSessionEvent):void {
//        ServiceManager.getUserSessionManagementService().findByContext(onOperationReturn, request);
     }

      [RequestHandler("{com.obas.events.RequestStageMenuDataEvent.REQUEST_ID}")]
      public static function onRequestStageMenuDataEvent(request:RequestStageMenuDataEvent):void {
         switch (request.actionType) {
            case RequestStageMenuDataEvent.TYPE_GET_MENU :
				ServiceManagerComEaseBusinessFlexRemoting.getWorkbenchManagementService().findMainMenusByPerspectiveViewCode((request.spec as Array)[0], onOperationReturn, request);
               break;
            case RequestStageMenuDataEvent.TYPE_GET_TOOL_BUTTON :
				ServiceManagerComEaseBusinessFlexRemoting.getWorkbenchManagementService().findCoolBarsByPerspectiveViewCode((request.spec as Array)[0], onOperationReturn, request);
               break;
         }
      }

   [RequestHandler("{com.obas.events.RequestPluginsEvent.REQUEST_ID}")]
   public static function onRequestPluginsEvent(request:RequestPluginsEvent):void {
      switch (request.actionType) {
         case RequestPluginsEvent.TYPE_GET_VIEW :
			 ServiceManagerComEaseBusinessFlexRemoting.getWorkbenchManagementService().findCoolViews(onOperationReturn, request);
            break;
         case RequestPluginsEvent.TYPE_GET_PERSPECTIVE :
			 ServiceManagerComEaseBusinessFlexRemoting.getWorkbenchManagementService().findCoolPerspectives(onOperationReturn, request);
            break;
        }
     }

   [RequestHandler("{com.obas.events.RequestChangePasswordEvent.REQUEST_ID}")]
   public static function onRequestChangePasswordEvent(request:RequestChangePasswordEvent):void {
//      ServiceManager.getUserManagementService().updatePassword(request.spec[0], request.spec[1], request.spec[2], onOperationReturn, request);
   }

    private static function onOperationReturn(event:MethodReturnEvent):void {
		ServiceManagerComObasNetworkFlexRemoting.onOperationReturn(getInstance(), event);
    }

   private static var _instance:DataOperationCommand = null;
   private static function getInstance():DataOperationCommand {
      if (_instance == null) {
         _instance = new DataOperationCommand();
         ViewRegistry.instance.eventBus.addParticipant(_instance, true);
      }

      return _instance;
   }

   }
}